//
//  CategoryTableView.m
//  EasyBuy
//
//  Created by Zhu Xiangjun on 10/10/12.
//  Copyright (c) 2012 shen donghao. All rights reserved.
//

#import "CategoryTableView.h"
#import "EasyBuy.h"
#import "EasyBuyService.h"
#import "CategoryData.h"
#import "CategoryTabelViewCell.h"

@implementation CategoryTableView
@synthesize categoryDataArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initCategoryViewData];
        
    }
    return self;
}

-(void) initCategoryViewData{
    EasyBuyService *service = [[EasyBuyService alloc] init];
    EasyBuyRequest<RequestProtocol> *request = [[EasyBuyRequest alloc] initCategoryRequest];
    [service executeCategoryRequest:request :RECEIVER_CATEGORY];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onReceivedCategoryData:)
                                                 name:RECEIVER_CATEGORY
                                               object:nil];
}

-(void)onReceivedCategoryData:(NSNotification *)notification{
    
    NSArray *_categoryDataArray = notification.object;
    categoryDataArray = _categoryDataArray;
    
    [self reloadData];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return [self.categoryDataArray count];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CategoryData *categoryData = [self.categoryDataArray objectAtIndex:indexPath.row];
    
    static NSString *CellIdentifier = @"cellId";
    CategoryTabelViewCell *cell = [self dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[CategoryTabelViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell initCellWithCategoryItemData:categoryData];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


@end
